package www.openteam.com.ui.qualityCntrl;


import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.adapter.qualityCntrl.IQCAdapter;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.IQCCust00;
import www.openteam.com.bean.IQCMsg00;
import www.openteam.com.bean.IQCSamplePlan;
import www.openteam.com.contract.qualityCntrl.IQCContract;
import www.openteam.com.presenter.qualityCntrl.IQCPresenter;
import www.openteam.com.tool.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class IQCFragment extends mBaseFragment<IQCPresenter>
        implements IQCContract.IQC00View {


    @Bind(R.id.iqc_spinner_sort)
    Spinner iqcSpinnerSort;
    @Bind(R.id.iqc_spinner_plan)
    Spinner iqcSpinnerPlan;
    @Bind(R.id.iqc_spinner_supplycode)
    Spinner iqcSpinnerSupplycode;
    @Bind(R.id.iqc_spinner_supplyName)
    TextView iqcSupplyName;
    @Bind(R.id.iqc_spinner_date)
    TextView iqcDate;
    @Bind(R.id.iqc_spinner_check)
    Spinner iqcSpinnerCheck;
    @Bind(R.id.iqc_note)
    TextView iqcNote;
    @Bind(R.id.query_btn)
    Button queryBtn;
    @Bind(R.id.iqc_Rcycleview)
    RecyclerView iqcRcycleview;
    @Bind(R.id.search_bar)
    ImageButton searchBar;
    private String TAG = getClass().getSimpleName();
    private List<IQCCust00.DataBeanX.DataBean> iqcCust00Data;
    private IQCAdapter iqcAdapter;
    public  CallBack callBack;

    public IQCFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_iqc, container, false);
        ButterKnife.bind(this, view);
        mPresenter = loadPresenter();
        initData();
        initView();
        initListener();
        return view;
    }

    public interface CallBack{

        void putData00(IQCMsg00.DataBeanX.DataBean dataBean);

        void putData01();
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    protected void initData() {

        if (mPresenter != null)
            mPresenter.attachView(this);
        ArrayList<String> typeList = new ArrayList<>();
        typeList.add("采购IQC");
        typeList.add("外发退料IQC");
        ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, typeList);
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        iqcSpinnerSort.setAdapter(typeAdapter);

    }

    @Override
    protected void initListener() {
        iqcDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iqcDate.setTag(iqcDate);
                selectCalendar(iqcDate);
            }
        });

        iqcSpinnerSupplycode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String tradName = iqcCust00Data.get(i).getTradName();
                iqcSupplyName.setText(tradName);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        queryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (iqcSpinnerSupplycode.getSelectedItem()==null){
                    ToastUtil.showToast(getContext(),"请输入供应商信息");
                }else {
                    createProgressDialog("正在查询");
                    String tradCode = iqcCust00Data.get(iqcSpinnerSupplycode.getSelectedItemPosition()).getTradCode();
                    mPresenter.findMsg00(tradCode);
                }
            }
        });
    }

    @Override
    protected void initView() {

        iqcRcycleview.setLayoutManager(new LinearLayoutManager(getContext()));
        iqcAdapter = new IQCAdapter(getContext());
        searchBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.findCust00();
                mPresenter.findSamplePalan();
            }
        });
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected IQCPresenter loadPresenter() {
        return new IQCPresenter();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {

    }

    public void createProgressDialog(String content) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getContext());
        }
        mProgressDialog.setMessage(content);
        mProgressDialog.show();


    }

    public void selectCalendar(final TextView v) {
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                v.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    @Override
    public void findSamplePalan(IQCSamplePlan iqcSamplePlan) {
        ArrayList<String> samplePlanList = new ArrayList<>();
        List<IQCSamplePlan.DataBeanX.DataBean> samplePalnData = iqcSamplePlan.getData().getData();
        for (IQCSamplePlan.DataBeanX.DataBean dataBean : samplePalnData) {
            samplePlanList.add(dataBean.getCode());
        }
        ArrayAdapter<String> samplePlanAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, samplePlanList);
        samplePlanAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        iqcSpinnerPlan.setAdapter(samplePlanAdapter);
    }

    /*
    采购IQC
     */
    @Override
    public void findCust00(IQCCust00 iqcCust00) {
        ArrayList<String> iqcCust00CodeList = new ArrayList<>();
        iqcCust00Data = iqcCust00.getData().getData();
        for (IQCCust00.DataBeanX.DataBean dataBean : iqcCust00Data) {
            iqcCust00CodeList.add(dataBean.getTradCode());
        }
        //厂商编号
        ArrayAdapter<String> iqcCust00CodeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, iqcCust00CodeList);
        iqcCust00CodeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        iqcSpinnerSupplycode.setAdapter(iqcCust00CodeAdapter);
    }

    @Override
    public void findMsg00(IQCMsg00 iqcMsg00) {
        mProgressDialog.dismiss();
        final List<IQCMsg00.DataBeanX.DataBean> data = iqcMsg00.getData().getData();
        iqcAdapter.setDataList(data);
        iqcRcycleview.setAdapter(iqcAdapter);
        //长按跳转到编辑界面
        iqcAdapter.setOnLongClickListener(new IQCAdapter.OnLongClickListener() {
            @Override
            public void onLongClick(int postion) {
                if (callBack!=null){
                    Log.e(TAG, "onLongClick: "+postion );
                    callBack.putData00(data.get(postion));
                }
            }
        });
    }

    @Override
    public void fail(String msg) {
        mProgressDialog.dismiss();
        Log.e(TAG, "fail: " + msg);
    }
}
